package servlet;


import service.impl.wenxianimpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Collection;
import java.util.UUID;

@WebServlet(name = "tiqu",urlPatterns = "/tiqu")
@MultipartConfig
public class tiqu extends HttpServlet {
    private service.impl.wenxianSerice wenxianSerice=new wenxianimpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String wenjia=null;
        // 创建保存上传文件的文件夹路径
        String savePath = "D:\\wenxian-master\\wenxian-master\\src\\main\\webapp\\upload\\";
        // 创建文件夹路径（如果不存在）
        File fileSaveDir = new File(savePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdirs();
        }
        // 从请求中获取所有的表单字段和文件部分
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            String filename = extractFileName(part);
            if (filename != null && !filename.equals("")) {
                // 保存文件到指定路径
                filename = UUID.randomUUID().toString() + "_" + filename;
                wenjia=filename;
                part.write(savePath + File.separator + filename);
            }
        }
        String a = "D:\\wenxian-master\\wenxian-master\\src\\main\\webapp\\upload\\" + wenjia;
        ce ce=new ce();
        String s = ce.pdf2(a);
        File file = new File(a); // 替换为要删除的文件的路径
file.delete();
        PrintWriter writer=response.getWriter();
        writer.print("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: Arial, sans-serif;\n" +
                "            line-height: 1.5;\n" +
                "            margin: 0 auto;\n" +
                "            max-width: 800px;\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "\n" +
                "        a {\n" +
                "            display: inline-block;\n" +
                "            padding: 12px 24px;\n" +
                "            background-color: #4CAF50;\n" +
                "            color: white;\n" +
                "            cursor: pointer;\n" +
                "            border-radius: 4px;\n" +
                "            font-size: 16px;\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "        h1 {\n" +
                "            font-size: 24px;\n" +
                "            color: #333;\n" +
                "            margin-bottom: 20px;\n" +
                "        }\n" +
                "\n" +
                "        p {\n" +
                "            font-size: 16px;\n" +
                "            color: #666;\n" +
                "            margin-bottom: 10px;\n" +
                "        }\n" +
                "\n" +
                "        .author {\n" +
                "            font-style: italic;\n" +
                "            color: #999;\n" +
                "        }\n" +
                "\n" +
                "        .date {\n" +
                "            color: #999;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<a href=\"z.html\" class=\"back-btn\"><i class=\"fas fa-arrow-left\"></i>返回</a><br><br>\n" +s+
                "</body>\n" +
                "</html>");


    }
    private String extractFileName(Part part) {
        String contentDisp = part.getHeader("content-disposition");
        String[] items = contentDisp.split(";");
        for (String item : items) {
            if (item.trim().startsWith("filename")) {
                return item.substring(item.indexOf("=") + 2, item.length() - 1);
            }
        }
        return null;
    }
}
